package fission;

import java.util.ArrayList;

    
/**
 *
 * @author MichaelAlbert
 */
public class FissionGame {

    private boolean[][] board;
    private int rows;
    private int columns;
    private ArrayList<FissionMove> moves;
    
    public FissionGame( boolean[][] board) {
        
        this.board = board;
        moves = new ArrayList<FissionMove>();
        rows = board.length;
        columns = board[0].length;
    }
    
    public boolean isValidMove(FissionMove move) {
        return
                move != null &&
                move.getRow() - move.dRow() >= 0 &&
                move.getRow() + move.dRow()  < rows &&
                move.getColumn() - move.dColumn() >= 0 &&
                move.getColumn() + move.dColumn() < columns &&
                board[move.getRow()][move.getColumn()] == Fission.STONE &&
                board[move.getRow() + move.dRow()][move.getColumn() + move.dColumn()] == Fission.SPACE &&
                board[move.getRow() - move.dRow()][move.getColumn() - move.dColumn()] == Fission.SPACE
                ;        
    }
    
    public boolean doMove(FissionMove move)  {
        
        if (!isValidMove(move)) {
            return false;
        }
       
        
        int r = move.getRow(); int dr = move.dRow();
        int c = move.getColumn(); int dc = move.dColumn();
        board[r][c] = Fission.SPACE;
        board[r+dr][c+dc] = Fission.STONE;
        board[r-dr][c-dc] = Fission.STONE;
        moves.add(move);
        return true;
        
    }
    
    public boolean undoLastMove() {
        
        if (moves.isEmpty()) return false;
        
        FissionMove move = moves.remove(moves.size()-1);
        int r = move.getRow(); int dr = move.dRow();
        int c = move.getColumn(); int dc = move.dColumn();
        board[r][c] = Fission.STONE;
        board[r+dr][c+dc] = Fission.SPACE;
        board[r-dr][c-dc] = Fission.SPACE;
        return true;
        
    }
    
    public int getRows() {
        return rows;
    }
    
    public int getColumns() {
        return columns;
    }
    
    public boolean getAt(int row, int col) {
        return board[row][col];
    }
    
    public boolean[][] getBoard() {
        boolean[][] result = new boolean[rows][columns];
        for(int r = 0; r < rows; r++) {
            System.arraycopy(board[r], 0, result[r], 0, columns);
        }
        return result;
    }
    
    public ArrayList<FissionMove> getMoves() {
        return moves;
    }
    
    @Override
    public String toString() {
        StringBuilder result = new StringBuilder();
        for(int r = board.length-1; r >= 0; r--) {
            for(int c = 0; c < board[r].length; c++) {
                if (board[r][c] == Fission.STONE) {
                    result.append('x');
                } else {
                    result.append('.');
                }
            }
            result.append('\n');
        }
        return result.toString();
    }
    public void printBoard(){
		for(int r = 0;r<getRows();r++){
			for(int c=0;c<getColumns();c++){
				System.out.print( board[r][c] + " ");
			}
			System.out.println();
		}
	}
}
